package cn.dujc.superkid.fragment.start;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cn.dujc.core.adapter.BaseQuickAdapter;
import cn.dujc.core.ui.BaseFragment;
import cn.dujc.core.util.ToastUtil;
import cn.dujc.superkid.R;
import cn.dujc.superkid.adapter.StartAdapter;
import cn.dujc.superkid.api.HttpHelper;
import cn.dujc.superkid.api.QMap;
import cn.dujc.superkid.api.Result;
import cn.dujc.superkid.api.ResultCallBack;
import cn.dujc.superkid.bean.RegisterResult;
import cn.dujc.superkid.bean.StartRankingResult;
import cn.dujc.superkid.utils.Preferences;

public class AllFragment extends BaseFragment {
    private RecyclerView recyclerView;
    private StartAdapter startAdapter;
    private List<StartRankingResult.RankingBean> rankingList = new ArrayList<>();
    private int page;


    @Override
    public int getViewId() {
        return R.layout.fragment_start;
    }

    @Override
    public void initBasic(Bundle savedInstanceState) {
        initView();
        initLogic();
        initData();
    }

    private void initData() {
        if (!Preferences.isLogin(this)) {
            return;
        }
        final RegisterResult loginResult = Preferences.getInstance().getLoginResult(mActivity);
        HttpHelper.getUserCenter().memberStarRanking(QMap.build()
                .put("user_id", loginResult.getUid())
                .put("channel", Preferences.getInstance().getChannel(mActivity).getChannel_id())
                .put("logincode", loginResult.getLogincode())
                .put("genre", "fs")
                .put("num", "20")
                .put("page", page)
                .create()).enqueue(new ResultCallBack<Result<StartRankingResult>>(mActivity) {
            @Override
            public void onSuccessCallback(int code, Result<StartRankingResult> body) {
                if (body.getData().getRanking().size() < 50) {
                    startAdapter.loadMoreEnd();
                }
                if (page == 0) {
                    startAdapter.setNewData(body.getData().getRanking());
                    int stl = body.getData().getUserInfo().getInClassPractice();
                    int csj = body.getData().getUserInfo().getSelfTestVolume();
                    int bs = body.getData().getUserInfo().getCup();
                    int ks = body.getData().getUserInfo().getTest();
                    int sum = body.getData().getUserInfo().getAnswer();
                    int sumRight = body.getData().getUserInfo().getAnswerCorrectly();
                    int star = body.getData().getUserInfo().getSelfTestVolume();
                    String text="我完成了"+stl+"次随堂练，"+csj+"次测试卷，"+bs+"次杯赛考试，"+ks+"次在线考试，总答题数"+sum+"，总答对数"+sumRight+"，我的星星数是"+star;
                    TextView headerText = new TextView(getActivity());
                    headerText.setTextColor(Color.parseColor("#666666"));
                    headerText.setTextSize(14);
                    headerText.setPadding(15,15,15,15);
                    headerText.setText(text);
                    startAdapter.addHeaderView(headerText);
                    if (body.getData().getRanking().size() < 50) {
                        startAdapter.loadMoreEnd();
                    }
                } else {
                    startAdapter.addData(body.getData().getRanking());
                    startAdapter.loadMoreComplete();
                }
            }

            @Override
            public void onFailureCallback(int code, String msg) {
                super.onFailureCallback(code, msg);
            }
        });
    }

    private void initLogic() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this.getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setNestedScrollingEnabled(false);
        startAdapter = new StartAdapter(rankingList);
        TextView tv = new TextView(getActivity());
        tv.setTextColor(Color.parseColor("#222222"));
        tv.setTextSize(15);
        startAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                page++;
                initData();
            }
        }, recyclerView);
        recyclerView.setAdapter(startAdapter);
    }

    private void initView() {
        recyclerView = (RecyclerView) findViewById(R.id.rv_start);
    }
}
